package com.fast.develop.integration.orm.converter;

import cn.hutool.core.date.DateUtil;
import com.fast.develop.meta.dm.MetaField;
import com.fast.develop.meta.dm.enums.BasicFieldType;
import com.fast.develop.meta.dm.field.FieldType;

import java.util.Date;


/**
 * @author 莫寅
 * @createTime 2022-06-20 12:00
 * @desc：
 */
public class DateConverter implements PersistenceConverter{


    @Override
    public Object conver(Object value, MetaField metaField){
        Object resultValue = value;
        if(metaField.getType().type() instanceof BasicFieldType){
            BasicFieldType basicFieldType = (BasicFieldType)metaField.getType().type();
            if(basicFieldType == BasicFieldType.Date){
                if(basicFieldType.getValue().equalsIgnoreCase("date")){
                    resultValue = dateConver(value);
                }
            }
        }

        return resultValue;
    }

    /**
     * 日期转换
     * @param value
     * @return
     */
    private Object dateConver(Object value){
        if(value instanceof String){
            String dateStr = (String)value;
            Date date = DateUtil.parseDate(dateStr);
            return date;
        }

        return value;
    }



    @Override
    public FieldType type() {
        return BasicFieldType.Date;
    }
}
